/*!
    \file  main.c
    \brief main flash program, erase
    
    \version 2017-12-26, V1.0.0, firmware for GD32E10x
*/

/*
    Copyright (c) 2017, GigaDevice Semiconductor Inc.

    All rights reserved.

    Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice, this 
       list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice, 
       this list of conditions and the following disclaimer in the documentation 
       and/or other materials provided with the distribution.
    3. Neither the name of the copyright holder nor the names of its contributors 
       may be used to endorse or promote products derived from this software without 
       specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
OF SUCH DAMAGE.
*/
#include "bspFMC.h"


void fmcErasePages(uint32_t address, uint16_t pageNum)
{
    uint32_t eraseCounter;

    /* unlock the flash program/erase controller */
    fmc_unlock();
	
#if BSP_MCU_TYPE == BSP_MCU_GD32E23x
    /* clear all pending flags */
    fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGAERR | FMC_FLAG_PGERR);
#elif BSP_MCU_TYPE == BSP_MCU_GD32F3x0
	fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
#endif   
	/* 计算块的首地址 */
	address /= FMC_PAGE_SIZE;
	address *= FMC_PAGE_SIZE;
    /* erase the flash pages */
    for(eraseCounter = 0; eraseCounter < pageNum; eraseCounter++)
	{
        fmc_page_erase(address + (FMC_PAGE_SIZE * eraseCounter));
		while(fmc_flag_get(FMC_FLAG_BUSY)){};
			
#if BSP_MCU_TYPE == BSP_MCU_GD32E23x			
        fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGAERR | FMC_FLAG_PGERR);
#elif BSP_MCU_TYPE == BSP_MCU_GD32F3x0
		fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
#endif 			
    }

    /* lock the main FMC after the erase operation */
    fmc_lock();
}


void fmcProgram(uint32_t address, uint8_t *pData, uint16_t dataNum)
{
	uint32_t uiData;	
	
    /* unlock the flash program/erase controller */
    fmc_unlock();

	while(dataNum)	
	{
		if(dataNum >= sizeof(uint32_t))
		{
			memcpy((void *)&uiData, pData,sizeof(uint32_t));
			dataNum -= sizeof(uint32_t);
		}
		else
		{
			memcpy((void *)&uiData, pData, dataNum);
			dataNum = 0;		
		}
        fmc_word_program(address, uiData);
        address += sizeof(uint32_t);
		pData += sizeof(uint32_t);
		while(fmc_flag_get(FMC_FLAG_BUSY)){};
			
#if BSP_MCU_TYPE == BSP_MCU_GD32E23x			
        fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGAERR | FMC_FLAG_PGERR);
#elif BSP_MCU_TYPE == BSP_MCU_GD32F3x0
		fmc_flag_clear(FMC_FLAG_END | FMC_FLAG_WPERR | FMC_FLAG_PGERR);
#endif 			
		if((address % FMC_PAGE_SIZE) == 0)//自动擦除
			fmcErasePages(address,1);
	}	

    /* lock the main FMC after the program operation */
    fmc_lock();
}

